package com.dmap.admin.service.backgroundv3.Impl;

import com.dmap.admin.controller.backgroundv3.param.IosVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.IosVersionParam;
import com.dmap.admin.controller.backgroundv3.param.IosVersionPublishParam;
import com.dmap.admin.dao.IosVersionMapper;
import com.dmap.admin.po.AppVersion;
import com.dmap.admin.po.IosVersion;
import com.dmap.admin.po.IosVersionExample;
import com.dmap.admin.service.backgroundv3.APPVersionService;
import com.dmap.admin.service.backgroundv3.IOSVersionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: ios版本
 **/
@Service
public class IOSVersionServiceImpl implements IOSVersionService {
    @Resource
    private IosVersionMapper iosVersionMapper;
    @Resource
    private APPVersionService appVersionService;
    @Transactional
    @Override
    public int insertIosVersion(IosVersionParam param) {
        IosVersion iosVersion = new IosVersion();
        if (param.getUrl() != null) {
            iosVersion.setUrl(param.getUrl());
        }
        if (param.getStatus() != null) {
            iosVersion.setStatus(param.getStatus());
        }
        if (param.getNewVersion() != null) {
            iosVersion.setNewVersion(param.getNewVersion());
        }
        if (param.getMinVersion() != null) {
            iosVersion.setMinVersion(param.getMinVersion());
        }
        if (param.getMd5() != null) {
            iosVersion.setMd5(param.getMd5());
        }
        if (param.getDescriptionEn() != null) {
            iosVersion.setDescriptionEn(param.getDescriptionEn());
        }
        if (param.getDescription() != null) {
            iosVersion.setDescription(param.getDescription());
        }
        if (param.getAppNumber() != null) {
            iosVersion.setAppNumber(param.getAppNumber());
        }
        iosVersion.setCreateTime(System.currentTimeMillis()/1000);
        iosVersion.setUpdateTime(System.currentTimeMillis()/1000);
        iosVersion.setUploadTime(System.currentTimeMillis()/1000);
        return iosVersionMapper.insertSelective(iosVersion);
    }
    @Transactional
    @Override
    public int updateIosVersion(IosVersionEditParam param) {
        IosVersion iosVersion = iosVersionMapper.selectByPrimaryKey(Long.parseLong(param.getIosVersionId()));
        if (iosVersion == null) {
            //没有该version
            return 2;
        }
        if (param.getUrl() != null) {
            iosVersion.setUrl(param.getUrl());
        }
        if (param.getStatus() != null) {
            iosVersion.setStatus(param.getStatus());
        }
        if (param.getNewVersion() != null) {
            iosVersion.setNewVersion(param.getNewVersion());
        }
        if (param.getMinVersion() != null) {
            iosVersion.setMinVersion(param.getMinVersion());
        }
        if (param.getMd5() != null) {
            iosVersion.setMd5(param.getMd5());
        }
        if (param.getDescriptionEn() != null) {
            iosVersion.setDescriptionEn(param.getDescriptionEn());
        }
        if (param.getDescription() != null) {
            iosVersion.setDescription(param.getDescription());
        }
        if (param.getAppNumber() != null) {
            iosVersion.setAppNumber(param.getAppNumber());
        }
        iosVersion.setUpdateTime(System.currentTimeMillis()/1000);
        iosVersion.setUploadTime(System.currentTimeMillis()/1000);
        return iosVersionMapper.updateByPrimaryKeySelective(iosVersion);
    }

    @Override
    public int deleteIosVersion(long id) {
        IosVersion iosVersion = iosVersionMapper.selectByPrimaryKey(id);
        if (iosVersion == null) {
            return 0;
        }
        //判断是否有最新版本app关联
        AppVersion appVersion = appVersionService.getAppVersionByAppNumber(iosVersion.getAppNumber());
        if(appVersion != null && appVersion.getAndroidVersion().equals(iosVersion.getNewVersion())){
            iosVersion.setNewVersion("");
            appVersionService.updateAppIosVersion(iosVersion);
        }
        return iosVersionMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    @Override
    public int deleteIosVersionList(String ids) {
        List<String> iosVersionList = Arrays.asList(ids.split(","));
        int count = 0;
        for (String id : iosVersionList) {
            count += deleteIosVersion(Long.parseLong(id));
        }
        if (count == iosVersionList.size()) {
            return 1;
        }
        return 0;
    }
    @Transactional
    @Override
    public int updateIosVersionPublish(IosVersionPublishParam param) {
        IosVersion iosVersion = iosVersionMapper.selectByPrimaryKey(Long.parseLong(param.getIosVersionId()));
        if (iosVersion == null) {
            //没有该version
            return 2;
        }
        //1.代表正常  2代表禁用
        if(iosVersion.getStatus() == 1){
            iosVersion.setStatus(2);
        }else{
            iosVersion.setStatus(1);
            //修改app最新版本
            appVersionService.updateAppIosVersion(iosVersion);
        }
        iosVersion.setUpdateTime(System.currentTimeMillis()/1000);
        return iosVersionMapper.updateByPrimaryKeySelective(iosVersion);

    }

    @Override
    public List<IosVersion> selectIosVersion() {
        IosVersionExample iosVersionExample = new IosVersionExample();
        IosVersionExample.Criteria criteria = iosVersionExample.createCriteria();
        //增加条件
        return iosVersionMapper.selectByExample(iosVersionExample);

    }

    @Override
    public int countIosVersion() {
        IosVersionExample iosVersionExample = new IosVersionExample();
        IosVersionExample.Criteria criteria = iosVersionExample.createCriteria();
        //增加条件
        return iosVersionMapper.countByExample(iosVersionExample);
    }

    @Override
    public IosVersion getIosVersion(long id) {
        return iosVersionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteIosVersionByAppNumber(String appNumber) {
        IosVersionExample iosVersionExample = new IosVersionExample();
        IosVersionExample.Criteria criteria = iosVersionExample.createCriteria();
        criteria.andAppNumberEqualTo(appNumber);
        return iosVersionMapper.deleteByExample(iosVersionExample);
    }
}
